/**
 * This file is part of Sonedyan.
 * 
 * Sonedyan is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation;
 * either version 3 of the License, or (at your option) any
 * later version.
 *
 * Sonedyan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Octave; see the file COPYING.  If not
 * see <http://www.gnu.org/licenses/>.
 * 
 * Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>
 */

package org.unige.mpej.eckmann.sonedyan.fns.classification;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.unige.mpej.eckmann.sonedyan.fns.bean.Actor.Status;
import org.unige.mpej.eckmann.sonedyan.utils.Statistics;

/**
 * Status classifier
 */
public class StatusClassifier
{
	private static Logger log = Logger.getLogger(org.unige.mpej.eckmann.sonedyan.fns.classification.StatusClassifier.class);
	
	/**
	 * get classification status accuracy
	 */
	public static double getClassificationAccuracy(String filePath)
	{
		Vector<Double> avgPhdTimeSeries = org.unige.mpej.eckmann.sonedyan.fns.utils.DataUtils.getAvgStatusTimeSeries(filePath, Status.PHD);
		Vector<Double> avgPostdocTimeSeries = org.unige.mpej.eckmann.sonedyan.fns.utils.DataUtils.getAvgStatusTimeSeries(filePath, Status.POSTDOC);
		Vector<Double> avgProfessorTimeSeries = org.unige.mpej.eckmann.sonedyan.fns.utils.DataUtils.getAvgStatusTimeSeries(filePath, Status.PROFESSOR);
		
		BufferedReader br = null;
		Vector<Double> avgTimeSeries = new Vector<Double>();
		double counter = 0;
		double correctlyClassified = 0;
		
		try
		{
			br = new BufferedReader(new FileReader(filePath));
			
			String line;
			
			while ((line = br.readLine()) != null)
			{
				if (line.length() > 0 && !line.startsWith("%") && !line.startsWith("@"))
				{
					String[] columns = line.split(",");
					Vector<Double> timeSeries = new Vector<Double>();
					String statusString = columns[columns.length - 1];
					
					counter++;
					
					for (int i = 0; i < columns.length - 1; i++)
					{
						timeSeries.add(new Double(columns[i]));	
					}
					
					double phdCorrelation = Statistics.sampleCorrelation(avgPhdTimeSeries, timeSeries);
					double postdocCorrelation = Statistics.sampleCorrelation(avgPostdocTimeSeries, timeSeries);
					double professorCorrelation = Statistics.sampleCorrelation(avgProfessorTimeSeries, timeSeries);
					
					double maxCorrelation = Math.max(phdCorrelation, Math.max(postdocCorrelation, professorCorrelation));
					
					if (maxCorrelation == phdCorrelation)
					{
						if (statusString.equals("PHD"))
							correctlyClassified++;
					}
					
					if (maxCorrelation == postdocCorrelation)
					{
						if (statusString.equals("POSTDOC"))
							correctlyClassified++;
					}
					
					if (maxCorrelation == professorCorrelation)
					{
						if (statusString.equals("PROFESSOR"))
							correctlyClassified++;
					}
				}
			} // while
			
			return correctlyClassified / counter;
		}
		catch(Exception e)
		{
			log.error("An error occured: " + e.getMessage());
			
			return 0;
		}
		finally
		{
			try
			{
				if (br != null)
				{ 
					br.close(); 
				}
			}
			catch(Exception e){}
		}
	}
	
}
